import json

from commonTool import dir_tool

dir_path = "d://json"


def get_dir_files():
    dir_list = dir_tool.DirTool(dir_path).get_dirs()
    print(dir_list)
    print("---------------")
    for path in dir_list:
        print(path)
        with open(path, 'r', encoding='utf-8') as file:
            data = file.read()
            progress_json(data)
            print("\n\n")


def get_file(file_path):
    print(file_path)
    with open(file_path, 'r', encoding='utf-8') as file:
        data = file.read()
        progress_json(data)
        print("\n\n")


def progress_json(json_data):
    save_key = False

    json_object = json.loads(json_data)
    entries = json_object["log"]["entries"]

    for entry in entries:
        resource_type = entry["_resourceType"]
        if resource_type == "xhr":
            response = entry["response"]
            content = response["content"]["text"]
            result = json.loads(content)
            datas = result["result"]
            # print(result)
            if isinstance(datas, dict) and 'datas' in datas and isinstance(datas['datas'], list):
                data_list = datas['datas']
                for data in data_list:
                    if data and not save_key:
                        save_key = True
                        print('\t'.join(data.keys()))
                    for value in data.values():
                        print(f'{value}' + '\t', end='')
                    print(end='\n')
        # for data in datas:
        #     if data and not save_key:
        #         save_key = True
        #         print('\t'.join(data.keys()))
        #     for value in data.values():
        #         print(f'{value}' + '\t', end='')
        #     print(end='\n')
        #     # print(category, code, name, desc, _type, values, sep="\t")


if __name__ == '__main__':
    get_file("D://json/iot.tuya.com.har1_all.har.json")

"""
电工照明 ✅
家居安防 ✅
生活电器 ✅
环境电器 ✅
厨卫电器 ✅
娱乐影音 ✅
运动健康 ✅
网络设备 ✅
其它 ✅
"""
